package com.cd.university.controller.typeOfAchievement.apply;

import com.alibaba.fastjson.JSONObject;
import com.cd.university.common.RestResult;
import com.cd.university.common.utils.MyIdGenerator;
import com.cd.university.db.pojo.Awarded;
import com.cd.university.service.AchievementProcessor;
import com.cd.university.service.impl.AchievementProcessorHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author 陈栋
 * @create 2021/11/3 16:42
 */
@RestController
@RequestMapping("/applyFile")
@Api("结题申请成果形式")
public class ApplyFileController {

    @Autowired
    private AchievementProcessorHolder achievementProcessorHolder;

    @GetMapping("/advance")
    @ApiOperation("预申请fileId")
    public RestResult advanceFileId() {
        return RestResult.success("申请的primaryId为")
                .put("data", MyIdGenerator.getInstance().getRandomNumber());
    }

    @PostMapping("/updateData/{type}")
    @ApiOperation("上传成果形式的数据")
    public RestResult updateDate(@RequestBody JSONObject jsonObject,
                                 @PathVariable("type") String type) {
        Object data = jsonObject.get("data");
        AchievementProcessor processor = achievementProcessorHolder.findApplyAchievementProcessor(type);
        return processor.applyData(data);
    }

    @PostMapping("/updateFile/{type}")
    @ApiOperation("上传成果文件")
    public RestResult updateFile(@RequestParam("file") MultipartFile file,
                                 @RequestParam("primaryId") String primaryId,
                                 @RequestParam("projectId") Integer projectId,
                                 @RequestParam("atId") Integer atId,
                                 @RequestParam("categoryId") Integer categoryId,
                                 @PathVariable("type") String type) {
        AchievementProcessor processor = achievementProcessorHolder.findApplyAchievementProcessor(type);
        return processor.applyFile(file, primaryId, projectId, atId, categoryId);
    }

}
